package cn.edu.nju.software.servlet.administrator;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.nju.software.model.administrator.BookingAtScene;

/**
 * Servlet implementation class BookingAtSceneServlet
 */
public class BookingAtSceneServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		PrintWriter writer = response.getWriter();
		
		request.setCharacterEncoding("UTF-8");
		
		String hall = request.getParameter("hall");
		String movie = request.getParameter("movie");
		String username = request.getParameter("username");
		int count = Integer.parseInt(request.getParameter("count"));
		
		String message = "";
		String reload = "";
		
		BookingAtScene booking = new BookingAtScene();
		if (username == null || username.equals("")) {
			message = "sucess!";
			booking.book(count, hall, movie);
		} else {
			if (booking.canBook(hall, movie, count, username)) {
				message = "sucess!";
				booking.book(username, count, hall, movie);
			} else {
				int flag = booking.getFlag();
				switch (flag) {
				case BookingAtScene.NT:
					message = "not enough tickets!";
					break;
				case BookingAtScene.NM:
					message = "not enough money!";
					break;
				}
			}
		}
		
		reload = "administrator/ticket.jsp";
		
		writer.println("<script>alert('" + message + "');"
					+ "window.location.href='" + reload +  "';</script>");
	}

}
